package com.qianfeng.day36_systemcamera;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.provider.MediaStore.Video.Thumbnails;
import android.view.View;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.VideoView;

public class MainActivity extends Activity {

	private ImageView iv;
	private VideoView vv;

	// private String path = Environment.getExternalStoragePublicDirectory(
	// Environment.DIRECTORY_DCIM).getAbsolutePath()
	// + File.separator + "a.mp4";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		iv = (ImageView) findViewById(R.id.iv);
		vv = (VideoView) findViewById(R.id.vv);

	}

	// 录制视频
	public void capture(View v) {

		Intent intent = new Intent();
		intent.setAction(MediaStore.ACTION_VIDEO_CAPTURE);
		// intent.putExtra(MediaStore.EXTRA_OUTPUT, value);
		startActivityForResult(intent, 0);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if (requestCode == 0 && resultCode == RESULT_OK) {
			// 存储的数据的uri.
			Uri uri = data.getData();
			Cursor cursor = getContentResolver().query(uri, null, null, null,
					null);
			while (cursor.moveToNext()) {

				// int columnCount = cursor.getColumnCount();
				// for (int i = 0; i < columnCount; i++) {
				// // 得到所有的列名
				// String columnName = cursor.getColumnName(i);
				// Log.i("TAG", "columnName=" + columnName);
				// }

				// 得到多媒体文件的路径
				String path = cursor.getString(cursor.getColumnIndex("_data"));

				// 播放视频的功能
				vv.setVideoPath(path);
				vv.setMediaController(new MediaController(this));
				vv.start();

				// 创建一个视频缩略图
				Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(path,
						Thumbnails.MINI_KIND);
				iv.setImageBitmap(bitmap);
			}
			
			cursor.close();
		}
	}

}
